﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Bas;
using nERP.Model;
using nERP.Model.Po;
using nERP.Model.Wms;

namespace nERP.ViewModel.Bas.GoodsVMs
{
    public partial class GoodsListVM : BasePagedListVM<Goods_View, GoodsSearcher>
    {

        protected override IEnumerable<IGridColumn<Goods_View>> InitGridHeader()
        {
            return new List<GridColumn<Goods_View>>{

                this.MakeGridHeader(x => x.Goods_GoodsCode).SetTitle(@Localizer["Page.商品编号"].Value),
                this.MakeGridHeader(x => x.Goods_GoodsName).SetTitle(@Localizer["Page.商品名称"].Value),
                this.MakeGridHeader(x => x.Goods_GoodsSpec).SetTitle(@Localizer["Page.规格型号"].Value),
                this.MakeGridHeader(x => x.Goods_GoodsType).SetTitle(@Localizer["Page.商品类别"].Value),
                this.MakeGridHeader(x => x.Goods_GoodsUnit).SetTitle(@Localizer["Page.计量单位"].Value),

                this.MakeGridHeader(x => x.Goods_GoodsImages).SetTitle(@Localizer["_Model._Goods._GoodsImages"].Value),

                this.MakeGridHeader(x => x.Goods_RestockingPrice).SetTitle(@Localizer["Page.进货价"].Value),
                this.MakeGridHeader(x => x.Goods_RetailPrice).SetTitle(@Localizer["Page.零售价"].Value),
                this.MakeGridHeader(x => x.Goods_WholesalePrice).SetTitle(@Localizer["Page.批发价"].Value),
                this.MakeGridHeader(x => x.Goods_SafetyStock).SetTitle(@Localizer["Page.安全库存"].Value),
                this.MakeGridHeader(x => x.Goods_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.Goods_Stock).SetTitle(@Localizer["Page.库存"].Value),
                this.MakeGridHeader(x => x.Goods_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.Goods_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.Goods_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.Goods_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.Goods_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<Goods_View> GetSearchQuery()
        {
            var query = DC.Set<Goods>()

                .CheckContain(Searcher.GoodsCode, x => x.GoodsCode)
                .CheckContain(Searcher.GoodsName, x => x.GoodsName)
                .CheckContain(Searcher.GoodsSpec, x => x.GoodsSpec)
                .CheckEqual(Searcher.GoodsTypeId, x => x.GoodsTypeId)
                .CheckEqual(Searcher.GoodsUnitId, x => x.GoodsUnitId)
                .CheckEqual(Searcher.RestockingPrice, x => x.RestockingPrice)
                .CheckEqual(Searcher.RetailPrice, x => x.RetailPrice)
                .CheckEqual(Searcher.WholesalePrice, x => x.WholesalePrice)
                .CheckEqual(Searcher.SafetyStock, x => x.SafetyStock)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.Stock, x => x.Stock)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new Goods_View
                {
                    ID = x.ID,

                    Goods_GoodsCode = x.GoodsCode,
                    Goods_GoodsName = x.GoodsName,
                    Goods_GoodsSpec = x.GoodsSpec,
                    Goods_GoodsType = DC.Set<GoodsClass>().Where(z0 => z0.Name == x.GoodsTypeId).Select(y => y.Name).FirstOrDefault(),
                    Goods_GoodsUnit = DC.Set<ComputationUnit>().Where(z0 => z0.UnitCode == x.GoodsUnitId).Select(y => y.UnitName).FirstOrDefault(),
                    Goods_GoodsImages = x.GoodsImages.Select(z0 => z0.FileId).ToList(),
                    Goods_RestockingPrice = x.RestockingPrice,
                    Goods_RetailPrice = x.RetailPrice,
                    Goods_WholesalePrice = x.WholesalePrice,
                    Goods_SafetyStock = x.SafetyStock,
                    Goods_Remark = x.Remark,

                    Goods_Stock = DC.Set<BuyInWarehouseDetail>().Where(z => z.GoodsId.Equals(x.GoodsCode) && z.Status == dStatusTypeEnum.Reviewed).Sum(x => x.BuyInNumber)
                    - DC.Set<BuyReturnOutWarehouseDetail>().Where(z => z.GoodsId.Equals(x.GoodsCode) && z.Status == dStatusTypeEnum.Reviewed).Sum(x => x.ReturnNumber)
                    + DC.Set<OtherInWarehouseDetail>().Where(z => z.GoodsId.Equals(x.GoodsCode) && z.Status == dStatusTypeEnum.Reviewed).Sum(x => x.OtherInNumber)
                    - DC.Set<SaleOutWarehouseDetail>().Where(z => z.GoodsId.Equals(x.GoodsCode) && z.Status == dStatusTypeEnum.Reviewed).Sum(x => x.SaleOutNumber)
                    + DC.Set<SaleReturnInWarehouseDetail>().Where(z => z.GoodsId.Equals(x.GoodsCode) && z.Status == dStatusTypeEnum.Reviewed).Sum(x => x.ReturnNumber)
                    - DC.Set<OtherOutWarehouseDetail>().Where(z => z.GoodsId.Equals(x.GoodsCode) && z.Status == dStatusTypeEnum.Reviewed).Sum(x => x.OtherOutNumber)
                    ,
                    Goods_CreateTime = x.CreateTime,
                    Goods_UpdateTime = x.UpdateTime,
                    Goods_CreateBy = x.CreateBy,
                    Goods_UpdateBy = x.UpdateBy,
                    Goods_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


    }
    public class Goods_View : Goods
    {

        public string Goods_GoodsCode { get; set; }
        public string Goods_GoodsName { get; set; }
        public string Goods_GoodsSpec { get; set; }
        public string Goods_GoodsType { get; set; }
        public string Goods_GoodsUnit { get; set; }
        public List<Guid> Goods_GoodsImages { get; set; }
        public decimal? Goods_RestockingPrice { get; set; }
        public decimal? Goods_RetailPrice { get; set; }
        public decimal? Goods_WholesalePrice { get; set; }
        public decimal? Goods_SafetyStock { get; set; }
        public string Goods_Remark { get; set; }
        public decimal? Goods_Stock { get; set; }
        public DateTime? Goods_CreateTime { get; set; }
        public DateTime? Goods_UpdateTime { get; set; }
        public string Goods_CreateBy { get; set; }
        public string Goods_UpdateBy { get; set; }
        public bool Goods_IsValid { get; set; }

    }

}